//
//  JYTSchoolHandler.m
//  JYT_IOS
//
//  Created by nxa-tankeke on 16/12/8.
//  Copyright © 2016年 com.yuan. All rights reserved.
//

#import "JYTSchoolHandler.h"
#import "JYTHttpClient.h"
#import "School.h"

@implementation JYTSchoolHandler
+ (JYTSchoolHandler *) shared
{
    static dispatch_once_t onceToken;
    static JYTSchoolHandler *schoolHandler;
    dispatch_once(&onceToken, ^{
        schoolHandler = [[JYTSchoolHandler alloc] init];
    });
    return schoolHandler;
}

- (void) getSchoolWithParam:(NSDictionary *)params WithHandler:(void (^)(NSArray *coaches, NSString *pageNo)) handler
{
    JYTHttpClient *client = [JYTHttpClient sharedInstance];
    
    
    BOOL isUsedOffLine = [[JYTUserManager shared] isUsedOffLine];
    
    if (!isUsedOffLine) {
 
        client.urlType = userCityUrl;
    } else {
        client.urlType = userCenterUrl;
    }

    
    client.finishCallbackHandler = ^(id result, NSString *code, NSString *msg) {
        
        NSArray *arr = result[@"data"];
        NSNumber *number = result[@"PageCount"];
       
        NSString *pageNo = [NSString stringWithFormat:@"%@", number];
        
        NSArray *schoolArr = [School mj_objectArrayWithKeyValuesArray:arr];
        
        handler(schoolArr, pageNo);
        
    };
    
    client.errorCallbackHandler = ^(NSString * code, NSString * msg) {
        handler(@[], nil);
    };
    
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setDictionary:params];
    
    //请求 参数
    //[param setObject:@"5" forKey:@"PageSize"];
    
    //请求 CommandCode
    if (!isUsedOffLine) {
         [param setObject:@"GetSchools" forKey:@"CommandCode"];
        
    } else {
        [param setObject:@"GetRecommendedSchools" forKey:@"CommandCode"];
    }

    
    [client post:@"GetDataInfo" withParam:param];
}

@end
